<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>作者博客：百度"three.js 郭隆邦"</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      /* 隐藏body窗口区域滚动条 */
    }
  </style>
  <!--引入three.js三维引擎-->
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
  <!-- 引入threejs扩展控件OrbitControls.js -->
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/controls/OrbitControls.js"></script>
</head>

<body>
  <!-- 复制meshphong_frag.glsl着色器代码，然后增加一个灰度计算的功能 -->
  <script id="fragmentShader" type="x-shader/x-fragment">
    #define PHONG

uniform vec3 diffuse;
uniform vec3 emissive;
uniform vec3 specular;
uniform float shininess;
uniform float opacity;

#include <common>
#include <packing>
#include <dithering_pars_fragment>
#include <color_pars_fragment>
#include <uv_pars_fragment>
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
#include <envmap_pars_fragment>
#include <gradientmap_pars_fragment>
#include <fog_pars_fragment>
#include <bsdfs>
#include <lights_pars_begin>
#include <lights_pars_maps>
#include <lights_phong_pars_fragment>
#include <shadowmap_pars_fragment>
#include <bumpmap_pars_fragment>
#include <normalmap_pars_fragment>
#include <specularmap_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>

void main() {

	#include <clipping_planes_fragment>

	vec4 diffuseColor = vec4( diffuse, opacity );
	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
	vec3 totalEmissiveRadiance = emissive;

	#include <logdepthbuf_fragment>
	#include <map_fragment>
	#include <color_fragment>
	#include <alphamap_fragment>
	#include <alphatest_fragment>
	#include <specularmap_fragment>
	#include <normal_fragment_begin>
	#include <normal_fragment_maps>
	#include <emissivemap_fragment>

	// accumulation
	#include <lights_phong_fragment>
	#include <lights_fragment_begin>
	#include <lights_fragment_maps>
	#include <lights_fragment_end>

	// modulation
	#include <aomap_fragment>

	vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;

	#include <envmap_fragment>

  // 原来的给片元赋值的代码注释，重新编写加入灰度计算功能
	// gl_FragColor = vec4( outgoingLight, diffuseColor.a );

  //计算RGB三个分量光能量之和，也就是亮度
  float luminance = 0.299*outgoingLight.r+0.587*outgoingLight.g+0.114*outgoingLight.b;
  //逐片元赋值，RGB相同均为亮度值，用黑白两色表达图片的明暗变化
  gl_FragColor = vec4(luminance,luminance,luminance,diffuseColor.a);

	#include <tonemapping_fragment>
	#include <encodings_fragment>
	#include <fog_fragment>
	#include <premultiplied_alpha_fragment>
	#include <dithering_fragment>

}
  </script>
  <script>
    /**
     * 创建场景对象Scene
     */
    var scene = new THREE.Scene();

    var geometry = new THREE.SphereGeometry(100, 25, 25);
    // 保留MeshPhongMaterial功能的前提下，增加一段灰度计算的功能
    var material = new THREE.ShaderMaterial({
      // 通过THREE.ShaderLib获得MeshPhongMaterial材质对象的uniforms值
      // 用于给着色器中的uniform变量传值
      uniforms: THREE.ShaderLib['phong'].uniforms,
      // 顶点着色器
      vertexShader: THREE.ShaderChunk['meshphong_vert'],
      // 片元着色器
      fragmentShader: document.getElementById('fragmentShader').textContent,
    });
    // 允许光照计算
    material.lights = true;

    //设置材质颜色
    material.color = new THREE.Color(0xffffff);
    // 设置材质贴图
    var loader = new THREE.TextureLoader()
    // 设置材质颜色贴图
    material.map = loader.load('./Earth.png')
    // 设置材质法线贴图
    material.normalMap = loader.load('./EarthNormal.png')


    // 设置着色器中的uniform变量对应的值，也就是uniforms属性的属性值

    // 着色器中diffuse变量对应的是材质color属性
    // 设置材质颜色
    material.uniforms.diffuse.value = material.color;

    // 设置颜色贴图map
    material.uniforms.map.value = material.map
    // 设置法线贴图
    material.uniforms.normalMap.value = material.normalMap
    // 设置凹凸深度
    material.uniforms.normalScale.value = new THREE.Vector2(3, 3);
    // 设置高光颜色属性specular
    material.uniforms.specular.value = new THREE.Color(0x444444);
    // 设置高亮程度
    material.uniforms.shininess.value = 20;

    var mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
    scene.add(mesh); //网格模型添加到场景中
    /**
     * 光源设置
     */
    //点光源
    var point = new THREE.PointLight(0xffffff, 0.9);
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    var point2 = new THREE.PointLight(0xffffff, 0.9);
    point2.position.set(-400, -200, -300); //点光源位置
    scene.add(point2); //点光源添加到场景中
    //环境光
    var ambient = new THREE.AmbientLight(0xffffff, 0.9);
    scene.add(ambient);
    /**
     * 相机设置
     */
    var width = window.innerWidth; //窗口宽度
    var height = window.innerHeight; //窗口高度
    var k = width / height; //窗口宽高比
    var s = 150; //三维场景显示范围控制系数，系数越大，显示的范围越大
    //创建相机对象
    var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(200, 300, 200); //设置相机位置
    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    /**
     * 创建渲染器对象
     */
    var renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setSize(width, height); //设置渲染区域尺寸
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象

    // 渲染函数
    function render() {
      renderer.render(scene, camera); //执行渲染操作
      // mesh.rotateY(0.01);//每次绕y轴旋转0.01弧度
      requestAnimationFrame(render); //请求再次执行渲染函数render，渲染下一帧
    }
    render();
    //创建控件对象  相机对象camera作为参数   控件可以监听鼠标的变化，改变相机对象的属性
    var controls = new THREE.OrbitControls(camera,renderer.domElement);
  </script>
</body>

</html>
